/**
 * Created by bolin on 2016/12/13.
 */
var Activity = function (activityType, position, canInfo, isload) {
    this.activityType = activityType;
    this.guid = Guid.NewGuid();
    this.ActivityProperty = {};
    this.ActivityProperty.guid = this.guid;
    this.ActivityProperty.ENABLE_RETURN = 0;
    this.ActivityProperty.ENABLE_RETURN_BY_PREVIOUS_PATH = 0;
    this.ActivityProperty.ENABLE_TRANSIT = 0;
    this.ActivityProperty.IS_ARTIFICIAL_DECISION = 0;
    this.ActivityProperty.IS_AUTOMATIC = 0;
    this.ActivityProperty.ENABLE_REMEMBER_DELIVER_PATH = 0;
    this.ActivityProperty.ENABLE_DELETE_SUB_FLOW = 0;
    this.ActivityProperty.FINISH_TYPE = 0;
    this.ActivityProperty.SUB_FLOW_TYPE = 0;
    this.ActivityProperty.FINISH_NUMBER = 0;
    this.ActivityProperty.activityType = activityType;
    this.ActivityProperty.isDel = 0;
    this.isDrawRuleStart = false;
    this.isDrawRule = false;
    this.position = position;
    this.Canvas = canInfo.canvas;
    this.Picture = canInfo.cxt;
    this.capture = {
        'iscapture': false,
        'point': new Point(0, 0)
    };
    switch (activityType) {
        case ActivityType.start:
            this.ActivityName = "开始";
            break;
        case ActivityType.drafting:
            this.ActivityName = "起草";
            break;
        case ActivityType.approval:
            this.ActivityName = "审批";
            break;
        case ActivityType.freedom:
            this.ActivityName = "自由";
            break;
        case ActivityType.branching:
            this.ActivityName = "分支";
            break;
        case ActivityType.merging:
            this.ActivityName = "合并";
            break;
        case ActivityType.subprocesses:
            this.ActivityName = "子流程";
            break;
        case ActivityType.condition:
            this.ActivityName = "条件";
            break;
        case ActivityType.end:
            this.ActivityName = "结束";
            break;
    }
    this.ClearModel = {};
    this.InitialClearModel();
    this.FromRule = new Array();
    this.ToRule = new Array();
    this.status = 2;
    this.isSelect = 2;
    wf.activityHelper.Activitys[wf.activityHelper.Activitys.length] = this;
    wf.activityHelper.Activitys[wf.activityHelper.Activitys.length - 1].ResetActivity();
    this.BranchingControl = null;
    this.MergingControl = null;
    this.ActivityProperty.activityName = this.ActivityName;
    if (isload == undefined && activityType == ActivityType.merging) {

        this.BranchingControl = wf.activityHelper.Activitys[wf.activityHelper.Activitys.length - 2];
        wf.activityHelper.Activitys[wf.activityHelper.Activitys.length - 2].MergingControl = this;
    }
}
Activity.prototype = {
    //更换ID
    /*应用场景：当打开一个流程，并在该流程的基础上编辑流程，并将编辑后的流程保存为一个全新的流程时，
     需要对原来流程中节点、规则等对象更换ID*/
    ChangeID: function () {
        var me = this;
        var tempActivityID = me.guid;
        me.guid = Guid.NewGuid();
        this.ActivityProperty.guid = me.guid;
        //RETURN
        if (me.ActivityProperty.WF_RULE != undefined) {
            $.each(me.ActivityProperty.WF_RULE, function (i, returnDto) {
                returnDto.id = Guid.NewGuid();
                returnDto.fromGuid = me.guid;
            });
            $.each(wf.activityHelper.Activitys, function (i, val) {
                if (val.ActivityProperty.WF_RULE != undefined && val.guid != me.guid) {
                    $.each(val.ActivityProperty.WF_RULE, function (j, returnDto) {
                        if (returnDto.toGuid == tempActivityID)
                            returnDto.toGuid = me.guid;
                    })
                }
            });
        }
        //PARTICIPANT
        if (me.ActivityProperty.activityParticipants != undefined) {
            $.each(me.ActivityProperty.activityParticipants, function (i, val) {
                val.guid = me.guid;
            });
        }
    },
    //设置节点的属性
    /*打开流程时，将节点的属性加载到系统中
     property：从数据库中读出的节点实体
     */
    SetProperty: function (property) {
        var me = this;
        this.ActivityProperty = property;
        if (property.guid != undefined)
            this.guid = property.guid;

        this.ActivityName = property.activityName
        //针对导入操作，如果模版中角色不存在当前系统中，则删除已选的角色
        if (me.ActivityProperty != undefined && me.ActivityProperty.activityParticipants != undefined) {
            var totalLength = me.ActivityProperty.activityParticipants.length - 1;
            for (h = totalLength; h >= 0; h--) {
                var isRemove = me.ActivityProperty.activityParticipants[h].type == 0;
                if (isRemove) {
                    $.each(SqlHelper.JobList, function (i, val) {
                        {
                            if (val.id == me.ActivityProperty.activityParticipants[h].participantId) {
                                isRemove = false;
                                return false;
                            }
                        }
                    });
                    if (isRemove)
                        me.ActivityProperty.activityParticipants.splice(h, 1);
                }
            }
        }

    },


    //在屏幕上绘制输出当前节点
    /*position：节点位置
     strokeStyle：边线样式
     fillStyle：填充样式
     */
    Draw: function (position, strokeStyle, fillStyle) {

        var activity = this;
        var cxt = this.Picture;
        activity.clearActivity();
        cxt.setTransform(1, 0, 0, 1, position.X, position.Y);
        cxt.strokeStyle = strokeStyle;
        switch (activity.activityType) {
            case ActivityType.approval: {
                activity.DrawRect(cxt, position, StyleDefine.Activity.Approval.activityRectVertexX, StyleDefine.Activity.Approval.activityRectVertexY, StyleDefine.Activity.Approval.activityRectWidth, StyleDefine.Activity.Approval.activityRectHeight, StyleDefine.Activity.Approval.activityRectAngle, StyleDefine.Activity.Approval.imgWidth, StyleDefine.Activity.Approval.imgHeight, strokeStyle, fillStyle, 'Approval');
            }
                break;
            case ActivityType.drafting: {
                activity.DrawRect(cxt, position, StyleDefine.Activity.Drafting.activityRectVertexX, StyleDefine.Activity.Drafting.activityRectVertexY, StyleDefine.Activity.Drafting.activityRectWidth, StyleDefine.Activity.Drafting.activityRectHeight, StyleDefine.Activity.Drafting.activityRectAngle, StyleDefine.Activity.Drafting.imgWidth, StyleDefine.Activity.Drafting.imgHeight, strokeStyle, fillStyle, 'Drafting');
            }
                break;
            case ActivityType.condition:
                activity.DrawDiamond(cxt, position, strokeStyle, fillStyle, StyleDefine.Activity.Condition.diamondWidth, StyleDefine.Activity.Condition.diamondHeight);
                break;
            case ActivityType.branching:
                activity.DrawDiamond(cxt, position, strokeStyle, fillStyle, StyleDefine.Activity.Branching.diamondWidth, StyleDefine.Activity.Branching.diamondHeight);
                break;
            case ActivityType.merging:
                activity.DrawDiamond(cxt, position, strokeStyle, fillStyle, StyleDefine.Activity.Merging.diamondWidth, StyleDefine.Activity.Merging.diamondHeight);
                break;
            case ActivityType.start: {
                var grd = cxt.createRadialGradient(-5, -5, 2, 0, 0, StyleDefine.Activity.Start.Radius);
                grd.addColorStop(0, "white");
                grd.addColorStop(1, "green");
                activity.DrawCircle(cxt, StyleDefine.Activity.Start.Radius, grd);
            }
                break;
            case ActivityType.end:
                var grd = cxt.createRadialGradient(-5, -5, 2, 0, 0, StyleDefine.Activity.End.Radius);
                grd.addColorStop(0, "white");
                grd.addColorStop(1, "red");

                this.DrawCircle(cxt, StyleDefine.Activity.End.Radius, grd);
                break;
            case ActivityType.freedom:
                this.DrawFreedom(StyleDefine.Activity.End.Radius);
                //this.DrawCircle(cxt, StyleDefine.Activity.End.Radius/2, 'rgba(225,0,0,255)');
                break;
        }
    },

    //清除图形的范围
    /*canvas重绘节点（比如拖动节点）时，需要先清除画布上原有图形，该方法用于定义清除画布上清除图像的范围*/
    InitialClearModel: function () {
        var me = this;
        me.ClearModel.offset = 2;
        switch (me.activityType) {
            case ActivityType.start:
                me.ClearModel.startPointX = -StyleDefine.Activity.Start.Radius - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Start.Radius - me.ClearModel.offset;
                me.ClearModel.width = me.ClearModel.heigth = 2 * StyleDefine.Activity.Start.Radius;
                break;
            case ActivityType.drafting:
                me.ClearModel.startPointX = -StyleDefine.Activity.Drafting.activityRectVertexX - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Drafting.activityRectVertexY - me.ClearModel.offset;
                me.ClearModel.width = 2 * StyleDefine.Activity.Drafting.activityRectVertexX;
                me.ClearModel.heigth = 2 * StyleDefine.Activity.Drafting.activityRectVertexY;
                break;
            case ActivityType.approval:
                me.ClearModel.startPointX = -StyleDefine.Activity.Approval.activityRectVertexX - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Approval.activityRectVertexY - me.ClearModel.offset;
                me.ClearModel.width = 2 * StyleDefine.Activity.Approval.activityRectVertexX;
                me.ClearModel.heigth = 2 * StyleDefine.Activity.Approval.activityRectVertexY;
                break;
            case ActivityType.freedom:
                me.ClearModel.startPointX = -StyleDefine.Activity.Start.Radius - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Start.Radius - me.ClearModel.offset;
                me.ClearModel.width = me.ClearModel.heigth = 2 * StyleDefine.Activity.Start.Radius;
                break;
            case ActivityType.branching:
                me.ClearModel.startPointX = -StyleDefine.Activity.Branching.diamondWidth - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Branching.diamondHeight - me.ClearModel.offset;
                me.ClearModel.width = 2 * StyleDefine.Activity.Branching.diamondWidth;
                me.ClearModel.heigth = 2 * StyleDefine.Activity.Branching.diamondHeight;
                break;
            case ActivityType.merging:
                me.ClearModel.startPointX = -StyleDefine.Activity.Merging.diamondWidth - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Merging.diamondHeight - me.ClearModel.offset;
                me.ClearModel.width = 2 * StyleDefine.Activity.Merging.diamondWidth;
                me.ClearModel.heigth = 2 * StyleDefine.Activity.Merging.diamondHeight;
                break;
            case ActivityType.Subprocesses:
                me.ClearModel.startPointX = -StyleDefine.Activity.Merging.diamondWidth - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Merging.diamondHeight - me.ClearModel.offset;
                me.ClearModel.width = 2 * StyleDefine.Activity.Merging.diamondWidth;
                me.ClearModel.heigth = 2 * StyleDefine.Activity.Merging.diamondHeight;
                break;
            case ActivityType.condition:
                me.ClearModel.startPointX = -StyleDefine.Activity.Condition.diamondWidth - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Condition.diamondHeight - me.ClearModel.offset;
                me.ClearModel.width = 2 * StyleDefine.Activity.Condition.diamondWidth;
                me.ClearModel.heigth = 2 * StyleDefine.Activity.Condition.diamondHeight;
                break;
            case ActivityType.end:
                me.ClearModel.startPointX = -StyleDefine.Activity.End.Radius - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.End.Radius - me.ClearModel.offset;
                me.ClearModel.width = me.ClearModel.heigth = 2 * StyleDefine.Activity.End.Radius;
                break;
        }
        me.ClearModel.startPointX -= 10;
        me.ClearModel.startPointY -= 10;
        me.ClearModel.width += 20;
        me.ClearModel.heigth += 20;

    },
    //清除图形
    /*清除画布上InitialClearModel中定义的范围*/
    clearActivity: function () {
        var me = this;
        var cxt = me.Picture;
        cxt.clearRect(me.ClearModel.startPointX, me.ClearModel.startPointY, me.ClearModel.width + me.ClearModel.offset * 2, me.ClearModel.heigth + me.ClearModel.offset * 2);
        if (me.captureX != null && me.captureX.iscapture)
            cxt.clearRect(me.captureX.point.X - me.ClearModel.width / 2 - me.ClearModel.offset - StyleDefine.Activity.capturelength,
                0,
                me.ClearModel.width + 2 * me.ClearModel.offset + 2 * StyleDefine.Activity.capturelength,
                StyleDefine.Window.height);
        if (me.captureY != null && me.captureY.iscapture)
            cxt.clearRect(0,
                me.captureY.point.Y - me.ClearModel.heigth / 2 - me.ClearModel.offset - StyleDefine.Activity.capturelength,
                StyleDefine.Window.width,
                me.ClearModel.heigth + 2 * me.ClearModel.offset + 2 * StyleDefine.Activity.capturelength);
        cxt.beginPath();
    },

    //绘制自由节点
    /*自由节点在流程图中用一个空心的圆圈表示
     r：圆圈的半径
     */
    DrawFreedom: function (r) {
        var me = this;
        var ctx = me.Picture;
        ctx.beginPath();
        ctx.arc(0, 0, r, 0, Math.PI * 2, true);
        ctx.closePath();
        ctx.stroke();
    },

    //设定节点的背景色
    /*tmp：指定的默认值
     y:节点高度/2
     */
    generateBackground: function (tmp, y) {
        var me = this;
        var fillStyle;

        if (tmp == undefined) {
            switch (me.isSelect) {

                case 0:
                    fillStyle = 'rgba(255,255,255,1)';
                    break;
                case 1:
                    var grd = me.Picture.createLinearGradient(0, -y, 0, y);
                    grd.addColorStop(0, "#e2eaf5");
                    grd.addColorStop(0.1, "#d9e6f8");
                    grd.addColorStop(0.1, "#cbdff7");
                    grd.addColorStop(0.21, "#daf2fd");
                    grd.addColorStop(0.21, "#bfdbff");
                    grd.addColorStop(0.21, "#b0cff7");
                    grd.addColorStop(0.1, "#dbe8f9");
                    grd.addColorStop(0.2, "#bfdbff");
                    grd.addColorStop(1, "#bfdbff");
                    fillStyle = grd;
                    break;
                case 2:
                    var grd = me.Picture.createLinearGradient(0, -y, 0, y);
                    grd.addColorStop(0, "#e2eaf5");
                    grd.addColorStop(0.1, "#d9e6f8");
                    grd.addColorStop(0.1, "#cbdff7");
                    grd.addColorStop(0.21, "#daf2fd");
                    grd.addColorStop(0.21, "#bfdbff");
                    grd.addColorStop(0.21, "#b0cff7");
                    grd.addColorStop(0.1, "#dbe8f9");
                    grd.addColorStop(0.2, "#bfdbff");
                    grd.addColorStop(1, "#bfdbff");
                    fillStyle = grd;
                    break;
                case 3:
                    var grd = me.Picture.createLinearGradient(0, -y, 0, y);
                    grd.addColorStop(0, "#fdcc91");
                    grd.addColorStop(0.5, "#fdb967");
                    grd.addColorStop(1, "#fbf76a");
                    fillStyle = grd;
                    break;

            }
        }
        else fillStyle = tmp;

        return fillStyle;

    },

    //绘制菱形
    /*分支节点、合并节点、条件节点用菱形表示，当绘制这几种图形时，调用此方法
     cxt：绘图对象
     position：中心位置
     strokeStyle：边线样式
     fillStyle：填充样式
     width：中心点到节点的距离
     height：中心点到节点的距离
     img：未用。。。。。。。
     */
    DrawDiamond: function (cxt, position, strokeStyle, fillStyle, width, height, img) {
        var me = this;
        me.DrawRuleStart();
        cxt.beginPath();
        cxt.save();
        cxt.shadowColor = "rgba(180,180,180,90)";
        //cxt.shadowOffsetX = 4;
        //cxt.shadowOffsetY = 4;
        //cxt.shadowBlur = 3;
        cxt.moveTo(-width, 0);
        cxt.lineTo(0, -height);
        cxt.lineTo(width, 0);
        cxt.lineTo(0, height);
        cxt.closePath();

        cxt.fillStyle = me.generateBackground(undefined, height);


        cxt.shadowOffsetX = 1;
        cxt.shadowOffsetY = 1;
        cxt.shadowBlur = 10;


        cxt.fill();
        cxt.restore();
        if (me.isDrawRuleStart)
            me.DrawRuleStart();
        if (me.activityType == ActivityType.merging || me.activityType == ActivityType.branching) {
            cxt.moveTo(-width + 5, 0);
            cxt.lineTo(width - 5, 0);
            if (me.activityType == ActivityType.branching) {
                cxt.moveTo(0, -height + 5);
                cxt.lineTo(0, height - 5);
            }
        }
        else if (me.activityType == ActivityType.condition) {
            if (fillStyle == null) fillStyle = 'rgba(0,0,0,1)';
            cxt.fillStyle = fillStyle;
            cxt.textAlign = "center";
            cxt.textBaseline = "middle";
            cxt.font = " 14px Microsoft YaHei";
            cxt.fillText(me.ActivityName, 0, 0);
        }
        cxt.stroke();

    },


    //绘制矩形
    /*起草节点、人工活动 使用矩形作为 表示图元
     cxt：绘图对象
     position：中心位置
     strokeStyle：边线样式
     fillStyle：填充样式
     width：中心点到节点的距离
     height：中心点到节点的距离
     img：未用。。。。。。。
     */
    DrawRect: function (cxt, position, vertexX, vertexY, width, height, angle, imgWidth, imgHeight, strokeStyle, fillStyle, img) {
        var me = this;
        var rect = cxt.roundRect(-vertexX, -vertexY, width, height, angle, true);
        cxt.fillStyle = me.generateBackground(undefined, height);
        rect.fill();
        cxt.restore();
        if (me.isDrawRuleStart)
            me.DrawRuleStart();
        var init = -vertexX + 25;
        var step = 15;
        cxt.fillStyle = fillStyle;
        cxt.font = " 14px Microsoft YaHei";
        cxt.textAlign = "center";
        cxt.textBaseline = "middle";
        var i = 0;
        var size = cxt.measureText(me.ActivityName);
        //一行
        if (size.width < 2 * vertexX - 10) {
            cxt.fillText(me.ActivityName, 0, 0);
        }
        //两行
        else if (size.width < 4 * vertexX - 20) {
            while (cxt.measureText(me.ActivityName.substring(0, i)).width <= size.width / 2) {
                ++i;
            }
            cxt.fillText(me.ActivityName.substring(0, i - 1), 0, -8);
            cxt.fillText(me.ActivityName.substring(i - 1, me.ActivityName.length), 0, 8);

        }//缩小文字间距
        else {
            while (cxt.measureText(me.ActivityName.substring(0, i)).width <= size.width / 2) {
                ++i;
            }
            cxt.fillText(me.ActivityName.substring(0, i - 1), 0, -8);
            cxt.fillText(me.ActivityName.substring(i - 1, me.ActivityName.length), 0, 8);
        }

        rect = cxt.roundRect(-vertexX, -vertexY, width, height, angle);
        rect.strokeStyle = strokeStyle;
        rect.stroke();

    },

    DrawStatus: function (status, coordinate, vertexY) {

        var cxt = this.Picture;
        var fillStyle = status == 1 ? '#00ff00' : '#ff0000';
        cxt.DrawCircle(coordinate, vertexY / 2, 5, fillStyle, false);

    },

    DrawCircle: function (cxt, r, style) {
        cxt.DrawCircle(0, 0, r, style, undefined, true);
    },

    ChangeSelectStatus: function (e) {
        if (this.status != this.isSelect || this.isDrawRuleStart != this.isDrawRule) {
            this.status = this.isSelect;
            this.isDrawRule = this.isDrawRuleStart
            this.ResetActivity();
            if (this.isSelect < 2)
                this.Canvas.css("z-index", "0");
        }
    },

    MoveActivity: function (e, istrans) {
        if (istrans == undefined)
            e = wf.mathHelper.Move(this.startMovePoint, this.currentActivityPosition, e);
        this.position = e;
        this.Draw(e, "rgba(0,0,0,1)", "rgba(0,0,0,1)");
        $.each(this.FromRule, function (i, val) {
            val.ReSetStartPoint(e);
        });
        $.each(this.ToRule, function (i, val) {
            val.ReSetEndPoint(e);
        });
    },

    SelectActivity: function (e, distinct) {

        var me = this;
        var tmpPoint = wf.canvasHelper.GetPoint(e);
        me.Canvas.css("z-index", "50");
        //设置当前操作节点
        wf.currentActivity = me;
        //设置当前节点样式
        me.ChangeSelectStatus(2, tmpPoint);
        //鼠标位置距离中心的距离，如果小于10，则更改当前选择操作为增加规则，否则仅仅选中当前节点
        if (me.CanHaveFromRule() && me.activityType != ActivityType.end && distinct < 10) {
            wf.gcommand = command.addrules;
            var canInfo = wf.canvasHelper.InitialCanvas();
            var rule = new Rule(me);
            wf.currentRule = rule;
        }
        else {
            wf.gcommand = command.selectactivity;
            wf.moveActivity = true;
            this.startMovePoint = tmpPoint;
            this.currentActivityPosition = me.position;
            if (this.activityType == ActivityType.branching) {
                this.MergingControl.ChangeSelectStatus(2, tmpPoint);
            }
            else if (this.activityType == ActivityType.merging) {
                this.BranchingControl.ChangeSelectStatus(2, tmpPoint);
            }
            wf.workspace.css("cursor", "move");
        }
    },

    ResetActivity: function () {
        var style = "rgba(0,0,0,1)";
        this.Draw(this.position, style, style);
    },

    DrawRuleStart: function () {
        if (this.activityType != ActivityType.start && this.activityType != ActivityType.end)
            this.DrawCircle(this.Picture, StyleDefine.Rule.SelectRadius, 'rgba(225,0,0,0.5)');
    },

    AddFromRule: function (rule) {
        this.FromRule[this.FromRule.length] = rule;
    },

    AddToRule: function (rule) {
        this.ToRule[this.ToRule.length] = rule;
    },

    CanHaveFromRule: function () {
        if (this.activityType == ActivityType.condition || this.activityType == ActivityType.branching || this.activityType == ActivityType.merging)
            return true;
        else if (this.activityType == ActivityType.end || this.activityType == ActivityType.freedom) return false;
        else {
            if (this.FromRule.length == 0) return true;
            else {
                var tmp = true;
                $.each(this.FromRule, function (i, val) {
                    if (val.endActivity.activityType != ActivityType.freedom) {
                        tmp = false;
                        return false;
                    }
                });
                return tmp;
            }
        }
    },

    CanHaveToRule: function () {
        var tmpAc = wf.currentRule.startActivity;
        var tmpFlag = true;
        for (m = 0; m < tmpAc.FromRule.length; m++)
            if (tmpAc.FromRule[m].endActivity.guid == this.guid)
                tmpFlag = false;
        return tmpFlag && this.activityType != ActivityType.start;
    },

    DrawFocus: function (point) {
        var cxt = this.Picture;
        cxt.beginPath();
        cxt.setTransform(1, 0, 0, 1, point.X, point.Y);
        cxt.strokeStyle = "rgba(225,0,0,1)";
        cxt.moveTo(-5, -5);
        cxt.lineTo(5, 5);
        cxt.moveTo(5, -5);
        cxt.lineTo(-5, 5);
        cxt.stroke();
    },

    DrawReferenceLine: function (currentPosition) {
        var me = this;
        var captureX = wf.canvasHelper.GetCaptureActivity('IsCaptureX', me.position, me);
        var captureY = wf.canvasHelper.GetCaptureActivity('IsCaptureY', me.position, me);
        var xd = false;
        var yd = false;
        this.captureX = captureX;
        this.captureY = captureY;
        var cxt = me.Picture;
        cxt.beginPath();
        if (captureX != null && captureX.iscapture) {
            cxt.DrawLine(new Point(captureX.point.X, 0), new Point(captureX.point.X, StyleDefine.Window.height), StyleDefine.Activity.referenceLineStyle);
            wf.canvasHelper.DrawFocusInvoke(me.Picture, captureX.activity);
            xd = true;
        }
        if (captureY != null && captureY.iscapture) {
            cxt.DrawLine(new Point(0, captureY.point.Y), new Point(StyleDefine.Window.width, captureY.point.Y), StyleDefine.Activity.referenceLineStyle);
            wf.canvasHelper.DrawFocusInvoke(me.Picture, captureY.activity);
            yd = true;
        }

        //存在交叉点,在交叉点出绘制
        if (xd || yd) {
            var point;
            if (xd && yd) {
                point = new Point(captureX.point.X, captureY.point.Y);
                wf.canvasHelper.DrawMovePoint(me.Picture, point, StyleDefine.Activity.capturelength, StyleDefine.Activity.referenceLineStyle);
            }
            else if (xd) {
                point = new Point(captureX.point.X, me.position.Y);
                wf.canvasHelper.DrawMovePoint(me.Picture, point, StyleDefine.Activity.capturelength, StyleDefine.Activity.referenceLineStyle);
            }
            else if (yd) {
                point = new Point(me.position.X, captureY.point.Y);
                wf.canvasHelper.DrawMovePoint(me.Picture, point, StyleDefine.Activity.capturelength, StyleDefine.Activity.referenceLineStyle);
            }
            this.capture = {
                'iscapture': true,
                'point': point
            }
        }
        else {
            this.capture.iscapture = false;
        }
    },
    GetActivityTypeName: function () {
        for (var i in ActivityType) {
            if (ActivityType[i] == this.activityType)
                return i;
        }
        return "Start"

    },
    //显示当前节点的属性窗口
    ShowProperty: function () {
        var that = this;
        var property = Property.InitailProperty(that);
        property.modal('show');
        $("#btnActivityPropOK").unbind().click(function () {
            Property.SaveData(that);
            property.modal('hide');
        });
    },
    //显示当前节点的属性窗口
    ShowProperty1: function () {
        var me = this;
        var property = Property.InitailProperty(me);

        var layerNum = $.layer({
            type: 1,
            title: false,
            move: [$("#divProptitle"), true],
            fix: false,
            closeBtn: false,
            shadeClose: true,
            shade: [0.5, '#000', true],
            border: [5, 0.3, '#666', true],
            offset: ['50%', '50%'],
            area: ['690px', '390px'],
            page: {dom: property},
            success: function () {
            }
        });
        $(".xubox_main").css("background-color", "transparent");
        $("#btnActivityPropOK").unbind();
        $("#btnActivityPropCancel").unbind();
        $("#btnActivityPropOK").click(function () {
            Property.SaveData(me);
            layer.close(layerNum);
        });
        $("#btnActivityPropCancel").click(function () {
            layer.close(layerNum);
        });

    },
    //释放当前节点
    /*
     force  是否强制注销，默认值undefined
     */
    Dispose: function (force) {
        var me = this;
        if (me && (force != undefined || (this.activityType != ActivityType.start && this.activityType != ActivityType.end && this.activityType != ActivityType.drafting))) {
            this.Canvas.remove();
            this.Canvas = null;
            wf.activityHelper.Activitys.Remove(this);
            for (j = this.FromRule.length - 1; j >= 0; j--) {
                this.FromRule[j].Dispose(force);
            }
            for (j = this.ToRule.length - 1; j >= 0; j--) {
                this.ToRule[j].Dispose(force);
            }
            if (force == undefined) {
                if (this.activityType == ActivityType.branching) {

                    this.MergingControl.Dispose('1');
                }
                else if (this.activityType == ActivityType.merging) {
                    this.BranchingControl.Dispose('1');
                }
            }
            me = undefined;
        } else {
            window.alert("此活动不能删除");
        }
    },
    //保存数据时验证节点设计的是否有问题
    /*
     draftingActivity 起草节点
     endActivity  结束节点
     */
    Validate: function (draftingActivity, endActivity) {
        var me = this;
        if (me.activityType == ActivityType.start || me.activityType == ActivityType.end)
            return {
                result: true,
                message: ''
            };
        var result = me.PreValidate(wf.activityHelper.DraftingActivity);
        if (result.result) {
            result = me.AfterValidate(wf.activityHelper.EndActivity.Preactivity);
            if (result.result) {
                if (me.activityType == ActivityType.approval || me.activityType == ActivityType.drafting) {
                    if (me.ActivityProperty.activityParticipants == undefined || me.ActivityProperty.activityParticipants.length == 0) {
                        result.result = false;
                        result.errorMessage = "[" + this.ActivityProperty.activityName + "]-->必须选人\r\n";
                    }
                }
            }
        }
        return result;
    },
    //验证当前节点是否有到开始节点的路径
    /*draftingActivity起草节点*/
    PreValidate: function (draftingActivity) {

        var me = this;
        if (me.guid == draftingActivity.guid)
            return {
                result: true,
                errorMessage: ''
            };
        var flag = true;
        var errorMessage = "";
        if (this.ToRule.length == 0) {
            errorMessage = "[" + me.ActivityProperty.activityName + "]-->无前驱节点\r\n";
            flag = false;
        }
        if (flag) {
            //  me.GetReturnActivitys(true, me.guid);
            if (me.Preactivity == undefined || !me.Preactivity.contains(draftingActivity)) {
                errorMessage = "[" + this.ActivityProperty.activityName + "]-->无法进入节点\r\n";
                flag = false;
            }
        }
        return {
            result: flag,
            errorMessage: errorMessage
        };
    },

    //验证当前节点是否有到结束节点的路径
    /*endActivity结束节点*/
    AfterValidate: function (endActivity) {


        var flag = true;
        var me = this;
        var errorMessage = '';
        if (me.activityType == ActivityType.freedom)
            return {
                result: flag,
                errorMessage: errorMessage
            };
        if (me.FromRule.length == 0 && me.activityType != ActivityType.freedom && me.activityType != ActivityType.subprocesses) {
            errorMessage = "[" + this.ActivityProperty.activityName + "]-->无后继节点\r\n";
            flag = false;
        }
        else if (!endActivity.contains(this)) {

            errorMessage = "[" + this.ActivityProperty.activityName + "]-->无法结束节点\r\n";
            flag = false;
        }
        return {
            result: flag,
            errorMessage: errorMessage
        };
    }
}

var Rule = function (startActivity) {
    var canInfo = wf.canvasHelper.InitialCanvas();
    this.guid = Guid.NewGuid();
    this.RuleProperty = {};
    this.RuleProperty.guid = this.guid;
    this.RuleProperty.ruleType = 0;
    this.RuleProperty.isDel = 0;
    this.RuleProperty.lineType = 0
    this.Points = new Array();
    this.startActivity = startActivity;
    this.endActivity = null;
    this.Canvas = canInfo.canvas;
    this.Picture = canInfo.cxt;
    this.isSelect = false;

    this.vextPosition = -1;
    this.capture = {
        iscapture: false,
        point: new Point(0, 0)
    }
}
Rule.prototype = {
    ChangeID: function () {
        var me = this;
        me.guid = Guid.NewGuid();
        me.RuleProperty.guid = me.guid;
    },

    SetProperty: function (property) {
        this.RuleProperty = property;
        if (property.guid != undefined) {
            this.guid = property.guid
            this.DrawRuleName('rgba(0,0,0,1)');
        }
    },

    DrawRule: function (currentPosition) {
        var me = this;
        var point;
        if (me.Points.length == 0) {
            point = wf.currentRule.GetIntersectionPoint(me.startActivity, currentPosition);
        }
        else {
            point = me.Points[0];
        }
        var cxt = me.Picture;
        cxt.strokeStyle = 'rgba(0,0,0,1)';
        this.restore();
        cxt.moveTo(point.X, point.Y);
        for (i = 1; i < me.Points.length; i++)
            cxt.lineTo(me.Points[i].X, me.Points[i].Y);
        cxt.lineTo(currentPosition.X, currentPosition.Y);
        var arrow = Arrow(me.Points.length == 0 ? point : me.Points[me.Points.length - 1], currentPosition);
        cxt.lineTo(arrow.upArrow.X, arrow.upArrow.Y);
        cxt.moveTo(currentPosition.X, currentPosition.Y);
        cxt.lineTo(arrow.downArrow.X, arrow.downArrow.Y);
        cxt.stroke();

    },

    generateBackground: function (tmp) {
        var me = this;
        var fillStyle;
        if (tmp == undefined) {
            fillStyle = 'rgba(0,0,0,1)';
        }
        else fillStyle = tmp;
        return fillStyle;
    },

    //重置路径
    restore: function () {
        var cxt = this.Picture;
        cxt.clearRect(-100, -100, StyleDefine.Window.width + 100, StyleDefine.Window.height + 100);
        cxt.restore();
        cxt.beginPath();
    },

    ResetRule: function () {
        var me = this;
        var style = "rgba(0,0,0,1)";
        if (this.isSelect)
            style = "rgba(255,0,0,1)";

        style = me.generateBackground(style);

        this.Draw(style);
        if (this.isSelect)
            this.DrawCircle();
    },

    Draw: function (strokeStyle) {
        var me = this;
        var point = me.Points[0];
        var cxt = me.Picture;
        me.restore();
        cxt.strokeStyle = strokeStyle;
        cxt.moveTo(point.X, point.Y);
        for (i = 1; i < me.Points.length; i++)
            cxt.lineTo(me.Points[i].X, me.Points[i].Y);
        var arrow = Arrow(me.Points[me.Points.length - 2], me.Points[me.Points.length - 1]);
        cxt.lineTo(arrow.upArrow.X, arrow.upArrow.Y);
        cxt.moveTo(me.Points[me.Points.length - 1].X, me.Points[me.Points.length - 1].Y);
        cxt.lineTo(arrow.downArrow.X, arrow.downArrow.Y);
        cxt.stroke();
        me.DrawRuleName(strokeStyle);
    },

    ChangeSelectStatus: function () {
        var me = this;
        if (me.status != me.isSelect) {
            me.status = me.isSelect;
            this.ResetRule();
        }
    },

    nulloperation: function () {
    },

    ReSetStartPoint: function (currentPosition) {
        var me = this;
        if (me.Points.length > 2) {
            var flag1 = me.IsHorizontal(me.Points[0], me.Points[1]) && me.IsVertical(me.Points[1], me.Points[2]);
            var flag2 = me.IsVertical(me.Points[0], me.Points[1]) && me.IsHorizontal(me.Points[2], me.Points[1]);
            if (flag1 || flag2) {

            }
            else {
                me.nulloperation();
            }
            if (flag1) {
                me.Points[1].Y = currentPosition.Y;
            }
            if (flag2) {
                me.Points[1].X = currentPosition.X;
            }
            if (me.Points.length == 3) {
                var pointE = me.GetIntersectionPoint(me.endActivity, me.Points[1]);
                var pointS = me.GetIntersectionPoint(me.startActivity, me.Points[1]);
                me.Points[0] = pointS;
                me.Points[2] = pointE;
            }
            me.Points[0] = me.GetIntersectionPoint(me.startActivity, me.Points[1]);
        }
        else {
            var pointE = me.GetIntersectionPoint(me.endActivity, me.startActivity.position);
            var pointS = me.GetIntersectionPoint(me.startActivity, pointE);
            me.Points[0] = pointS;
            me.Points[1] = pointE;
        }
        me.ResetRule();
    },

    ReSetEndPoint: function (currentPosition) {
        var me = this;
        if (me.Points.length > 2) {
            if (me.IsHorizontal(me.Points[me.Points.length - 1], me.Points[me.Points.length - 2])
                && me.IsVertical(me.Points[me.Points.length - 2], me.Points[me.Points.length - 3])
            ) {
                me.Points[me.Points.length - 2].Y = currentPosition.Y;
            }
            if (me.IsVertical(me.Points[me.Points.length - 1], me.Points[me.Points.length - 2]) &&
                me.IsHorizontal(me.Points[me.Points.length - 2], me.Points[me.Points.length - 3])) {
                me.Points[me.Points.length - 2].X = currentPosition.X;
            }
            if (me.Points.length == 3) {
                var pointE = me.GetIntersectionPoint(me.endActivity, me.Points[1]);
                var pointS = me.GetIntersectionPoint(me.startActivity, me.Points[1]);
                me.Points[0] = pointS;
                me.Points[2] = pointE;
            }
            else
                me.Points[me.Points.length - 1] = me.GetIntersectionPoint(me.endActivity, me.Points[me.Points.length - 2]);
        }
        else {
            var pointE = me.GetIntersectionPoint(me.endActivity, me.startActivity.position);
            var pointS = me.GetIntersectionPoint(me.startActivity, pointE);
            me.Points[0] = pointS;
            me.Points[1] = pointE;
        }
        me.ResetRule();
    },
    IsHorizontal: function (p1, p2) {
        if (Math.abs(p1.Y - p2.Y) < 2)
            return true;
        return false;
    },

    IsVertical: function (p1, p2) {
        if (Math.abs(p1.X - p2.X) < 2)
            return true;
        return false;
    },
    ResetPoint: function (e) {
        var me = this;
        var point = wf.canvasHelper.GetPoint(e);
        if (me.pointPosition != -1) {
            var ishandler = false;
            if (me.Points.length > 3) {
                if (me.vextPosition > 0 && me.vextPosition < me.Points.length - 1) {
                    if (me.vextPosition < me.Points.length - 2) {
                        if (me.IsHorizontal(me.Points[me.vextPosition - 1], me.Points[me.vextPosition]) && me.IsHorizontal(me.Points[me.vextPosition + 1], me.Points[me.vextPosition + 2])) {
                            me.Points[me.vextPosition].X = point.X;
                            me.Points[me.vextPosition + 1].X = point.X;
                            //me.SetStartEndPoint(point);
                            ishandler = true;
                        }
                        else if (me.IsVertical(me.Points[me.vextPosition - 1], me.Points[me.vextPosition]) && me.IsVertical(me.Points[me.vextPosition + 1], me.Points[me.vextPosition + 2])) {
                            me.Points[me.vextPosition].Y = point.Y;
                            me.Points[me.vextPosition + 1].Y = point.Y;
                            //me.SetStartEndPoint(point);
                            ishandler = true;
                        }
                    }
                    else if (me.vextPosition > 1) {
                        if (me.IsHorizontal(me.Points[me.vextPosition + 1], me.Points[me.vextPosition]) && me.IsHorizontal(me.Points[me.vextPosition - 1], me.Points[me.vextPosition - 2])) {
                            me.Points[me.vextPosition].X = point.X;
                            me.Points[me.vextPosition - 1].X = point.X;
                            //me.SetStartEndPoint(point);
                            ishandler = true;
                        }
                        else if (me.IsVertical(me.Points[me.vextPosition + 1], me.Points[me.vextPosition]) && me.IsVertical(me.Points[me.vextPosition - 1], me.Points[me.vextPosition - 2])) {
                            me.Points[me.vextPosition].Y = point.Y;
                            me.Points[me.vextPosition - 1].Y = point.Y;
                            //me.SetStartEndPoint(point);
                            ishandler = true;
                        }
                    }
                }
            }

            if (!ishandler) {
                me.Points[me.vextPosition] = point;
                me.SetStartEndPoint(point);
            }
            this.ResetRule();
            this.DrawCircle();
        }
    },
    SetStartEndPoint: function (point) {
        var me = this;
        if (me.vextPosition == 1) {
            me.Points[0] = me.GetIntersectionPoint(me.startActivity, point);
        }
        if (me.vextPosition == me.Points.length - 2) {
            me.Points[me.Points.length - 1] = me.GetIntersectionPoint(me.endActivity, point);
        }
    },

    AddPoint: function (point) {
        var me = this;
        if (me.capture.iscapture)
            point = me.capture.point;
        if (me.Points.length == 0)
            me.Points[0] = me.GetIntersectionPoint(me.startActivity, point);
        me.Points[me.Points.length] = point;
    },

    DrawCircle: function () {
        var rule = this;
        var cxt = rule.Picture;
        rule.ChangeSelectStatus(true);
        for (i = 0; i < rule.Points.length; i++) {
            cxt.DrawCircle(rule.Points[i].X, rule.Points[i].Y, 5, 'rgba(225,0,0,0.5)');
        }
    },
    SelectRule: function (result) {
        this.DrawCircle();
        wf.gcommand = command.selectrule;
        if (result.selectType == 1) {
            wf.moveRule = true;
            wf.workspace.css("cursor", "move");
        }
    },
    //判断当前规则是否被鼠标点中
    IsSelect: function (point) {
        var result = {
            'isSelect': false,
            'selectedObject': null,
            'distinct': 1000,
            'vextdistinct': 1000,
            'selectType': 0
        };
        for (i = 1; i < this.Points.length; i++) {
            var dis = wf.mathHelper.GetNearestDistance(this.Points[i - 1], this.Points[i], point);
            if (dis < result.distinct)
                result.distinct = dis;
            var vextdistinct = wf.mathHelper.PointDistinct(point.X, point.Y, this.Points[i].X, this.Points[i].Y);
            var position = i;
            if (i == 1) {
                var vextdistinct1 = wf.mathHelper.PointDistinct(point, this.Points[0]);
                if (vextdistinct1 < vextdistinct) {
                    vextdistinct = vextdistinct1;
                    position = 0;
                }
            }
            if (vextdistinct < result.vextdistinct) {
                result.vextdistinct = vextdistinct;
                this.vextPosition = position;
            }

        }
        if (result.distinct < 2 || result.vextdistinct < 5) {
            result.isSelect = true;
            result.selectedObject = this;
            result.selectType = result.vextdistinct < 5 ? 1 : 0;
        }


        return result;
    },

    ChangeActivity: function (point, result) {
        var me = this;
        if (me.vextPosition == me.Points.length - 1) {
            //更改endActivity
            if (result.isSelect) {
                me.endActivity.ToRule.Remove(this);
                me.endActivity = result.selectedObject;
                me.endActivity.ToRule[me.endActivity.ToRule.length] = this;
            }
            me.ReSetEndPoint(point);
        }
        else if (me.vextPosition == 0) {
            if (result.isSelect && result.selectedObject.CanHaveFromRule()) {

                me.startActivity.FromRule.Remove(this);
                me.startActivity = result.selectedObject;
                me.startActivity.FromRule[me.startActivity.FromRule.length] = this;
            }
            me.ReSetStartPoint(point);

        }
    },
    //获取所有线段中最长的一条
    DrawRuleName: function (strokeStyle) {

        var me = this;
        if (me.RuleProperty != undefined && me.RuleProperty.ruleName != undefined && me.RuleProperty.ruleName != '') {
            var tmp = 0;
            var dis = 0;
            var a = 0;
            while (a < me.Points.length - 1) {
                var tmpDis = wf.mathHelper.PointDistinct(me.Points[a], me.Points[++a]);
                if (tmpDis > dis) {
                    dis = tmpDis;
                    tmp = a;
                }
            }
            var p1;
            var p2;
            if (tmp > 0) {
                if (me.Points[tmp - 1].X > me.Points[tmp].X) {
                    p2 = me.Points[tmp - 1];
                    p1 = me.Points[tmp];
                }
                else {
                    p1 = me.Points[tmp - 1];
                    p2 = me.Points[tmp];
                }
                //文字旋转角度
                var angle;

                var sincos = wf.mathHelper.Angle(p1, p2);
                //0~180
                if (sincos.sin > 0) {
                    angle = Math.acos(sincos.cos);
                }
                //180~360
                else if (sincos.sin < 0) {
                    angle = 2 * Math.PI - Math.acos(sincos.cos);
                }
                else {
                    if (sincos.cos == 1) angle = 0;
                    else if (sincos.cos == -1) angle = Math.PI;
                }
                me.Picture.save();
                me.Picture.translate(p1.X + Math.cos(angle) * dis / 3 + 10 * +Math.sin(angle), p1.Y + Math.sin(angle) * dis / 3 - 10 * Math.cos(angle));
                me.Picture.rotate(angle);
                me.Picture.fillStyle = strokeStyle;
                me.Picture.fillText(me.RuleProperty.ruleName, 0, 0);
                me.Picture.restore();
            }
        }
    },

    GetIntersectionPoint: function (activity, point) {

        if (activity.activityType == ActivityType.start || activity.activityType == ActivityType.end || activity.activityType == ActivityType.freedom) {
            var r;
            if (activity.activityType == ActivityType.start) {
                r = StyleDefine.Activity.Start.Radius;
            }
            else if (activity.activityType == ActivityType.end) {
                r = StyleDefine.Activity.End.Radius;
            }

            else if (activity.activityType == ActivityType.freedom) {
                r = StyleDefine.Activity.End.Radius;
            }
            point = new Point(point.X - activity.position.X, point.Y - activity.position.Y);
            var dis = wf.mathHelper.PointDistinct(point.X, point.Y, 0, 0);

            var sin = point.X / dis;
            var cos = point.Y / dis;

            var resultX = r * sin + activity.position.X;
            var resultY = r * cos + activity.position.Y;
            return new Point(resultX, resultY);


        }

        else if (activity.activityType == ActivityType.condition || activity.activityType == ActivityType.merging || activity.activityType == ActivityType.branching) {
            var p1;
            var p2;

            var width;
            var height;

            if (activity.activityType == ActivityType.condition) {
                width = StyleDefine.Activity.Condition.diamondWidth;
                height = StyleDefine.Activity.Condition.diamondHeight;
            }
            else if (activity.activityType == ActivityType.merging) {
                width = StyleDefine.Activity.Merging.diamondWidth;
                height = StyleDefine.Activity.Merging.diamondHeight;
            }
            else if (activity.activityType == ActivityType.branching) {
                width = StyleDefine.Activity.Branching.diamondWidth;
                height = StyleDefine.Activity.Branching.diamondHeight;
            }


            if (point.X > activity.position.X) {
                //右侧偏下
                if (point.Y > activity.position.Y) {
                    p1 = new Point(0, height);
                    p2 = new Point(width, 0);
                }
                else if (point.Y == activity.position.Y) {
                    return new Point(activity.position.X + width, activity.position.Y)

                }
                //右侧偏上
                else {
                    p1 = new Point(0, -height);
                    p2 = new Point(width, 0);

                }
            }
            else if (point.X == activity.position.X) {
                if (point.Y > activity.position.Y)
                    return new Point(activity.position.X, activity.position.Y + height);
                else return new Point(activity.position.X, activity.position.Y - height);
            }
            else {
                //左侧偏上
                if (point.Y > activity.position.Y) {
                    p1 = new Point(-width, 0);
                    p2 = new Point(0, height);
                }
                else if (point.Y == activity.position.Y) {
                    return new Point(activity.position.X - width, activity.position.Y);
                }

                else {
                    p1 = new Point(-width, 0),
                        p2 = new Point(0, -height)
                }
            }

            return wf.mathHelper.GetIntersectionPoint(activity.position, point, p1, p2);

        }

        else {

            var activityRectVertexY;
            var activityRectVertexX;
            var r;
            if (activity.activityType == ActivityType.drafting) {
                activityRectVertexY = StyleDefine.Activity.Drafting.activityRectVertexY;
                activityRectVertexX = StyleDefine.Activity.Drafting.activityRectVertexX;
                r = StyleDefine.Activity.Drafting.activityRectAngle;
            }
            else if (activity.activityType == ActivityType.approval) {
                activityRectVertexY = StyleDefine.Activity.Approval.activityRectVertexY;
                activityRectVertexX = StyleDefine.Activity.Approval.activityRectVertexX;
                r = StyleDefine.Activity.Approval.activityRectAngle;
            }
            //以当前顶点为坐标系
            var relativeCoordinates = new Point(point.X - activity.position.X, point.Y - activity.position.Y);
            var me = this;
            var k = 0;

            if (relativeCoordinates.X != 0) {
                k = relativeCoordinates.Y / relativeCoordinates.X;
            }
            //鼠标在节点的右侧
            if (relativeCoordinates.X > 0) {
                //                    activityRectVertexX
                //                    activityRectVertexY
                //右侧偏下
                if (relativeCoordinates.Y > 0) {
                    var y1 = activityRectVertexY;
                    var x1 = y1 / k;
                    var result = new Point(x1, y1);
                    var center = new Point(activityRectVertexX - r, activityRectVertexY - r);
                    if (x1 > activityRectVertexX) {
                        //在弧上
                        result.X = activityRectVertexX;
                        result.Y = k * result.X;
                    }
                    if (result.X > center.X && result.Y > center.Y) {

                        var tmp = wf.mathHelper.LineCircleIntersection(center, r, k, relativeCoordinates);
                        if (tmp != null)
                            result = tmp;
                    }
                    return result.MovePoint(activity.position);

                }

                else if (relativeCoordinates.Y == 0) {
                    return new Point(activity.position.X + activityRectVertexX, activity.position.Y);
                }
                //右侧偏上
                else {
                    var y1 = -activityRectVertexY;

                    var x1 = y1 / k;

                    var result = new Point(x1, y1);

                    var center = new Point(activityRectVertexX - r, -activityRectVertexY + r);

                    if (result.X > center.X) {
                        result.X = activityRectVertexX;
                        result.Y = k * result.X;
                    }

                    if (result.X > center.X && result.Y < center.Y) {
                        var tmp = wf.mathHelper.LineCircleIntersection(center, r, k, relativeCoordinates);
                        if (tmp != null)
                            result = tmp;
                    }

                    return result.MovePoint(activity.position);
                }
            }
            else if (relativeCoordinates.X == 0) {
                if (relativeCoordinates.Y > 0) {
                    return new Point(activity.position.X, activity.position.Y + activityRectVertexY);
                }
                else {
                    return new Point(activity.position.X, activity.position.Y - activityRectVertexY);
                }

            }
            else {
                //左侧偏上
                if (relativeCoordinates.Y > 0) {
                    var y1 = activityRectVertexY;
                    var x1 = y1 / k;
                    var result = new Point(x1, y1);
                    var center = new Point(-activityRectVertexX + r, activityRectVertexY - r);
                    if (x1 < -activityRectVertexX) {
                        result.X = -activityRectVertexX;
                        result.Y = k * result.X;
                    }

                    if (result.X < center.X && result.Y > center.Y) {

                        var tmp = wf.mathHelper.LineCircleIntersection(center, r, k, relativeCoordinates);
                        if (tmp != null)
                            result = tmp;
                    }
                    return result.MovePoint(activity.position);
                }
                else if (relativeCoordinates.Y == 0) {
                    return new Point(activity.position.X - activityRectVertexX, activity.position.Y);
                }

                else {
                    var y1 = -activityRectVertexY;
                    var x1 = y1 / k;
                    var result = new Point(x1, y1);
                    var center = new Point(-activityRectVertexX + r, -activityRectVertexY + r);
                    if (x1 < -activityRectVertexX) {
                        if (x1 < -activityRectVertexX + r) {
                            result.X = -activityRectVertexX;
                            result.Y = k * result.X;
                        }
                    }
                    if (result.X < center.X && result.Y < center.Y) {
                        var tmp = wf.mathHelper.LineCircleIntersection(center, r, k, relativeCoordinates);
                        if (tmp != null) result = tmp;
                    }
                    return result.MovePoint(activity.position);
                }
            }
        }

    },

    //currentPosition,鼠标当前位置
    DrawReferenceLine: function (currentPosition) {
        var me = this;
        var count = 0;
        var point;
        if (me.Points.length == 0)
            point = me.startActivity.position;
        else point = me.Points[me.Points.length - 1];
        me.IsCapture(currentPosition, point)
        var xd = false;
        var yd = false;


        var captureX = wf.canvasHelper.GetCaptureActivity('IsCaptureX', currentPosition);


        var captureY = wf.canvasHelper.GetCaptureActivity('IsCaptureY', currentPosition);

        if (captureX != null && captureX.iscapture) {
            me.Picture.DrawLine(new Point(captureX.point.X, 0), new Point(captureX.point.X, StyleDefine.Window.height), StyleDefine.Activity.referenceLineStyle);
            wf.canvasHelper.DrawFocusInvoke(me.Picture, captureX.activity);
            if (this.capture.direct == 'y')
                xd = true;
        }
        if (captureY != null && captureY.iscapture) {
            me.Picture.DrawLine(new Point(0, captureY.point.Y), new Point(StyleDefine.Window.width, captureY.point.Y), StyleDefine.Activity.referenceLineStyle);
            wf.canvasHelper.DrawFocusInvoke(me.Picture, captureY.activity);
            if (this.capture.direct == 'x')
                yd = true;
        } //存在交叉点,在交叉点出绘制


        var point = null;
        if (this.capture.iscapture && xd)
            point = new Point(captureX.point.X, this.capture.point.Y);
        else if (this.capture.iscapture && yd)
            point = new Point(this.capture.point.X, captureY.point.Y);
        else if (this.capture.iscapture)
            point = this.capture.point;
        else if (xd)
            point = captureX.point;
        else if (yd)
            point = captureY.point;
        if (point != null) {
            this.capture.iscapture = true;
            this.capture.point = point;
            this.DrawMovePoint(point);
        }
    },

    //currentPosition当前位置
    //point参考点
    IsCapture: function (currentPosition, point) {
        var cxt = this.Picture;
        cxt.strokeStyle = StyleDefine.Rule.referenceLineStyle;
        cxt.restore();
        cxt.beginPath();

        this.capture = {
            'iscapture': false,
            'point': null,
            'direct': null
        }

        if (wf.mathHelper.PointDistinct(currentPosition, new Point(point.X, currentPosition.Y)) < 10) {
            cxt.moveTo(point.X, 0);
            cxt.lineTo(point.X, StyleDefine.Window.height);
            this.capture = {
                'iscapture': true,
                'point': new Point(point.X, currentPosition.Y),
                'direct': 'x'
            }
            cxt.stroke();
        }
        else if (wf.mathHelper.PointDistinct(currentPosition, new Point(currentPosition.X, point.Y)) < 10) {
            cxt.moveTo(0, point.Y);
            cxt.lineTo(StyleDefine.Window.width, point.Y);
            this.capture = {
                'iscapture': true,
                'point': new Point(currentPosition.X, point.Y),
                'direct': 'y'
            }
            cxt.stroke();
        }
        return this.capture;
    },

    DrawMovePoint: function (point) {
        var x1 = point.X - StyleDefine.Rule.capturelength;
        var y1 = point.Y - StyleDefine.Rule.capturelength;
        var cxt = this.Picture;
        cxt.strokeStyle = StyleDefine.Rule.strokeStyle;
        cxt.beginPath();
        cxt.rect(x1, y1, 2 * StyleDefine.Rule.capturelength, 2 * StyleDefine.Rule.capturelength);
        cxt.stroke();
    },
    ShowProperty: function () {
        var that=this;
        var property = $('#ruleProperty');
        property.modal();
        ruleProperty.InitialPage($('#ruleProperty .widget-main'), that);
        $("#btnRulePropOK").click(function () {
            ruleProperty.SaveData(that);
            property.modal('hide');
        });
    },
    ShowProperty1: function () {

        var me = this;


        var layerNum = $.layer({
            type: 1,
            title: false,
            move: [$("#divRulePropertyTitle"), true],
            fix: false,
            closeBtn: false,
            shadeClose: true,
            shade: [0.5, '#000', true],
            border: [5, 0.3, '#666', true],
            offset: ['50%', '50%'],
            area: ['690px', '190px'],
            page: {dom: $("#divRuleProperty")},
            success: function () {
            }
        });
        $(".xubox_main").css("background-color", "transparent");
        ruleProperty.InitialPage(this);
        $("#btnRulePropertyOK").unbind();
        $("#btnRulePropertyCancel").unbind();
        $("#btnRulePropertyOK").click(function () {

            ruleProperty.SaveData(me);
            layer.close(layerNum);
        });
        $("#btnRulePropertyCancel").click(function () {
            layer.close(layerNum);
        });


    },

    Dispose: function (force) {
        var me = this;
        if (force != undefined || me.startActivity.activityType != ActivityType.start) {
            me.startActivity.FromRule.Remove(this);
            if (me.endActivity != undefined)
                me.endActivity.ToRule.Remove(this);
            me.Canvas.remove();
            me.Canvas = null;
            wf.rules.Remove(me);
        }
        else {
            window.alert("此规则不能删除");
        }
    },

    Validate: function () {
    }
}

